package com.fairytask.ui.effect
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.define.MoneyDefine;
	import com.fairytask.engine.define.ResConfig;
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;

	/**
	 * 获取金钱效果
	 * @author xujh
	 * 
	 */
	public class MoneyEffect extends Sprite
	{
		//效果名称
		public var effectName	:String	=	"";
		
		private var _mc		:Bitmap;	//图片
		private var _iconStr	:String = "";	//图标string
		private var _postArr	:Array	= [];	//位置数据
		private var itemSpace	:Sprite;
		private var func		:Function;
		
		//金钱飞往目标位置
		private var SILVER_X	:int = 110;
		private var SILVER_Y	:int = 40;
		private var GOLD_X	:int = 110;
		private var GOLD_Y	:int = 55;
		
		/**
		 * @param type 金钱类型
		 * @param startX 开始播放的位置
		 * @param startY 开始播放的位置
		 * @param onComplete 完成后的事件
		 * 
		 */
		public function MoneyEffect(type:int=0, startX:int=480, startY:int=280, onComplete:Function=null)
		{
			_postArr = [startX,startY];
			getMoneyIcon(type);
			effectName = _iconStr;
			func = onComplete;
			//加载图片
			//QueueLoader.StartLoad(_iconStr,onLoad);
		}
		
		/**
		 * 根据金钱种类获取图标 
		 * @param type
		 * 
		 */
		private function getMoneyIcon(type:int):void
		{
			switch(type)
			{
				//银两
				case MoneyDefine.CAPITAL_TYPE_SILVER:
					_iconStr = ResConfig.moneyIcon + "silver.png";
					_postArr.push(SILVER_X - GameStage.mainLayer.x);
					_postArr.push(SILVER_Y - GameStage.mainLayer.y);
					break;
				//仙玉
				case MoneyDefine.CAPITAL_TYPE_GOLD:
					_iconStr = ResConfig.moneyIcon + "gold.png";
					_postArr.push(GOLD_X - GameStage.mainLayer.x);
					_postArr.push(GOLD_Y - GameStage.mainLayer.y);
					break;
			}
		}
		
		/**
		 * 加载完成
		 * @param val
		 * 
		 */
		private function onLoad(val:Bitmap):void
		{
			_mc = val;
			_mc.smoothing=true;
			init();
		}
		
		private function init():void
		{
			itemSpace = new Sprite();
			itemSpace.x=_postArr[0];
			itemSpace.y=_postArr[1];
			itemSpace.addChild(_mc);
			addChild(itemSpace);
			itemSpace.alpha = 0;
			TweenLite.to(itemSpace,1,{alpha:1,y:_postArr[1],onComplete:onAlpha});
		}
		
		/**
		 * 播放动画 
		 * 
		 */
		private function onAlpha():void
		{
			//弧线锚定位置				终点位置			
			TweenLite.to(itemSpace,3,{bezierThrough:[{x:400, y:200},{x:_postArr[2], y:_postArr[3]}],ease:Cubic.easeOut,onComplete:onComplete});			
		}
		
		private function onComplete():void
		{
			if(null != func)
			{
				func();
			}
			this.removeChild(itemSpace);
		}
		
		public function show():void
		{
			GameStage.uiSpace.addTop(this);
		}
		
		public function hide():void
		{
			if(null != this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}